<?php


namespace app\system\service;

class BlacklistService
{
	public static function getlist($options)
	{
		$where[] = ["appid", "=", $options["appid"]];
		if (isset($options["type"]) && $options["type"] != "") {
			$where[] = ["type", "=", $options["type"]];
		}
		if (isset($options["value"]) && $options["value"] != "") {
			$where[] = ["value", "=", $options["value"]];
		}
		$count = \think\Db::name("app_blacklist")->where($where)->count();
		$pages = ceil($count / $options["limit"]);
		$list = \think\Db::name("app_blacklist")->where($where)->page($options["page"], $options["limit"])->order("id", "desc")->select();
		$data = [];
		foreach ($list as $item) {
			$data[] = $item;
		}
		return ["code" => \app\system\model\Code::success, "data" => $data, "count" => $count, "pages" => $pages];
	}
	public static function getinfo($options)
	{
		$item = \think\Db::name("app_blacklist")->where(["id" => $options["id"], "appid" => $options["appid"]])->find();
		if (empty($item)) {
			return ["code" => \app\system\model\Code::fail, "msg" => "ID不存在"];
		}
		return ["code" => \app\system\model\Code::success, "data" => $item];
	}
	public static function add($options)
	{
		\think\Db::startTrans();
		try {
			$row = \think\Db::name("app_blacklist")->where(["appid" => $options["appid"], "type" => $options["type"], "value" => $options["value"]])->find();
			if (!empty($row)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "该记录已存在"];
			}
			$data = ["appid" => $options["appid"], "type" => $options["type"], "value" => $options["value"], "remarks" => $options["remarks"] ?? "", "addtime" => time()];
			\think\Db::name("app_blacklist")->insert($data);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function edit($options)
	{
		\think\Db::startTrans();
		try {
			$row = \think\Db::name("app_blacklist")->where(["id" => $options["id"]])->where(["appid" => $options["appid"]])->find();
			if (empty($row)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
			}
			$row = \think\Db::name("app_blacklist")->where(["appid" => $options["appid"], "type" => $options["type"], "value" => $options["value"]])->find();
			if (!empty($row) && $row["id"] != $options["id"]) {
				return ["code" => \app\system\model\Code::fail, "msg" => "该记录已存在"];
			}
			$data = ["type" => $options["type"], "value" => $options["value"], "remarks" => $options["remarks"] ?? ""];
			\think\Db::name("app_blacklist")->where(["id" => $options["id"], "appid" => $options["appid"]])->update($data);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function del($options)
	{
		\think\Db::startTrans();
		try {
			$row = \think\Db::name("app_blacklist")->where(["id" => $options["id"]])->where(["appid" => $options["appid"]])->find();
			if (empty($row)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
			}
			\think\Db::name("app_blacklist")->where(["id" => $options["id"]])->delete();
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
}